package com.runvp.rest.biz;

import com.runvp.common.annotation.Log;
import com.runvp.common.annotation.ParamValid;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.core.page.TableDataInfo;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.enums.PayChannel;
import com.runvp.common.enums.TranType;
import com.runvp.common.utils.ShiroUtils;
import com.runvp.common.utils.poi.ExcelUtil;
import com.runvp.service.biz.domain.BizCustomerTranLog;
import com.runvp.service.biz.domain.BizCustomerWallet;
import com.runvp.service.biz.service.IBizCustomerWalletService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户钱包Controller
 *
 * @author runvp
 * @date 2022-11-05
 */
@Controller
@RequestMapping("/biz/wallet")
public class BizCustomerWalletController extends BaseController {
    private String prefix = "biz/wallet";

    @Autowired
    private IBizCustomerWalletService bizCustomerWalletService;

    @RequiresPermissions("biz:wallet:view")
    @GetMapping()
    public String wallet(ModelMap mmap, Long customerId) {
        mmap.put("customerId", customerId);
        return prefix + "/wallet";
    }

    /**
     * 查询用户钱包列表
     */
    @RequiresPermissions("biz:wallet:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizCustomerWallet bizCustomerWallet) {
        startPage();
        List<BizCustomerWallet> list = bizCustomerWalletService.selectBizCustomerWalletList(bizCustomerWallet);
        return getDataTable(list);
    }
    /**
     * 导出用户钱包列表
     */
    @RequiresPermissions("biz:wallet:export")
    @Log(title = "用户钱包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizCustomerWallet bizCustomerWallet) {
        List<BizCustomerWallet> list = bizCustomerWalletService.selectBizCustomerWalletList(bizCustomerWallet);
        ExcelUtil<BizCustomerWallet> util = new ExcelUtil<BizCustomerWallet>(BizCustomerWallet.class);
        return util.exportExcel(list, "用户钱包数据");
    }

    /**
     * 新增用户钱包
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户钱包
     */
    @ParamValid
    @RequiresPermissions("biz:wallet:add")
    @Log(title = "用户钱包", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizCustomerWallet bizCustomerWallet) {
        return toAjax(bizCustomerWalletService.insertBizCustomerWallet(bizCustomerWallet));
    }

    /**
     * 充值
     */
    @RequiresPermissions("biz:wallet:edit")
    @GetMapping("/recharge/{customerId}")
    public String recharge(@PathVariable("customerId") Long customerId, ModelMap mmap) {
        mmap.put("customerId", customerId);
        return prefix + "/recharge";
    }

    /**
     * 充值保存
     */
    @ParamValid
    @ResponseBody
    @RequiresPermissions("biz:wallet:edit")
    @PostMapping("/recharge/save")
    public AjaxResult rechargeSave(BizCustomerTranLog bizCustomerTranLog) {
        bizCustomerTranLog.setTranType(TranType.WALLET.getCode());
        bizCustomerTranLog.setPayChannel(PayChannel.PLAT.getCode());
        bizCustomerTranLog.setCreateBy(ShiroUtils.getLoginName());
        return AjaxResult.success(bizCustomerWalletService.rechargeSave("平台操作:",bizCustomerTranLog));

    }

    /**
     * 修改用户钱包
     */
    @RequiresPermissions("biz:wallet:edit")
    @GetMapping("/edit/{customerId}")
    public String edit(@PathVariable("customerId") Long customerId, ModelMap mmap) {
        BizCustomerWallet bizCustomerWallet = bizCustomerWalletService.selectBizCustomerWalletByCustomerId(customerId);
        mmap.put("bizCustomerWallet", bizCustomerWallet);
        return prefix + "/edit";
    }

    /**
     * 用户钱包详情
     */
    @RequiresPermissions("biz:wallet:edit")
    @GetMapping("/detail/{customerId}")
    public String detail(@PathVariable("customerId") Long customerId, ModelMap mmap) {
        BizCustomerWallet bizCustomerWallet = bizCustomerWalletService.selectBizCustomerWalletByCustomerId(customerId);
        mmap.put("bizCustomerWallet", bizCustomerWallet);
        return prefix + "/detail";
    }

    /**
     * 修改保存用户钱包
     */
    @ParamValid
    @RequiresPermissions("biz:wallet:edit")
    @Log(title = "用户钱包", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizCustomerWallet bizCustomerWallet) {
        return toAjax(bizCustomerWalletService.updateBizCustomerWallet(bizCustomerWallet));
    }

    /**
     * 删除用户钱包
     */
    @RequiresPermissions("biz:wallet:remove")
    @Log(title = "用户钱包", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bizCustomerWalletService.deleteBizCustomerWalletByCustomerIds(ids));
    }
}
